//
//  FLMManageOrderHeaderView.m
//  FLMSuperS
//
//  Created by xiaoyang on 16/9/16.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMManageOrderHeaderView.h"
#import "FLMManageOrderHeaderCell.h"
#import "HJCarouselViewLayout.h"

@interface FLMManageOrderHeaderView()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    CGRect superFrame;
    NSIndexPath *_curIndexPath;
}
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) UIImageView *bgImageView;
@property (nonatomic,strong) NSArray *titleArr;

@end

@implementation FLMManageOrderHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        superFrame = frame;
        [self layoutUI];
    }
    
    return self;
}

- (void)configViewWithDataArr:(NSArray *)dataArr
{
    _titleArr = dataArr;
    [self.collectionView reloadData];
    [self performSelector:@selector(leftScroll) withObject:nil afterDelay:0.5];
}

- (void)layoutUI
{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, superFrame.size.height-70, ScreenW, 70)];
        _bgImageView.image = [UIImage imageNamed:@"manag_order_bg"];
        [self addSubview:_bgImageView];
    }

    
    if (!_collectionView) {
        HJCarouselViewLayout *layout = [[HJCarouselViewLayout alloc] initWithAnim:HJCarouselAnimLinear];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(90*screenRatio, 312*90*screenRatio/273);
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(20, 0, superFrame.size.width-40, superFrame.size.height) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        [_collectionView registerClass:[FLMManageOrderHeaderCell class] forCellWithReuseIdentifier:@"FLMManageOrderHeaderCell"];
        [self addSubview:_collectionView];
    }
    
    
}

- (NSIndexPath *)curIndexPath {
    NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
    NSIndexPath *curIndexPath = nil;
    NSInteger curzIndex = 0;
    for (NSIndexPath *path in indexPaths.objectEnumerator) {
        UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:path];
        if (!curIndexPath) {
            curIndexPath = path;
            curzIndex = attributes.zIndex;
            continue;
        }
        if (attributes.zIndex > curzIndex) {
            curIndexPath = path;
            curzIndex = attributes.zIndex;
        }
    }
    return curIndexPath;
}


- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *curIndexPath = [self curIndexPath];
    if (indexPath.row == curIndexPath.row) {
        return YES;
    }
    
    if (curIndexPath.row>indexPath.row) {
        [self rightScroll];
    }else{
        [self leftScroll];
    }
    
    return NO;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.didSelectBlock) {
        self.didSelectBlock(indexPath.row);
    }
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.titleArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    FLMManageOrderHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FLMManageOrderHeaderCell" forIndexPath:indexPath];
     NSLog(@"%@----%@",_curIndexPath,indexPath);
    [cell configCellWithTitle:@"" iconImage:self.titleArr[indexPath.row] isSelceted:_curIndexPath.row == indexPath.row];
    return cell;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSIndexPath *curIndexPath = [self curIndexPath];
    _curIndexPath = curIndexPath;
    NSLog(@"%@",_curIndexPath);
    [self.collectionView reloadData];
    if (self.didSelectBlock) {
        self.didSelectBlock(curIndexPath.row);
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSIndexPath *curIndexPath = [self curIndexPath];
    _curIndexPath = curIndexPath;
    NSLog(@"%@",_curIndexPath);
    [self.collectionView reloadData];
    if (self.didSelectBlock) {
        self.didSelectBlock(curIndexPath.row);
    }
}

#pragma mark - 私有方法

- (void)leftScroll
{
    NSIndexPath *curIndexPath = [self curIndexPath];
    if (curIndexPath.row== self.titleArr.count-1) {
        return;
    }
    HJCarouselViewLayout *layout = (HJCarouselViewLayout *)self.collectionView.collectionViewLayout;
    CGFloat cellWidth = layout.itemSize.width;
    CGFloat cellHeight = layout.itemSize.height;
    CGRect visibleRect = CGRectZero;
    visibleRect = CGRectMake(cellWidth * curIndexPath.row + cellWidth, 0, cellWidth, cellHeight);
    [self.collectionView scrollRectToVisible:visibleRect animated:YES];
}

- (void)rightScroll
{
    NSIndexPath *curIndexPath = [self curIndexPath];
    if (curIndexPath.row== 0) {
        return;
    }
    HJCarouselViewLayout *layout = (HJCarouselViewLayout *)self.collectionView.collectionViewLayout;
    CGFloat cellWidth = layout.itemSize.width;
    CGFloat cellHeight = layout.itemSize.height;
    CGRect visibleRect = CGRectZero;
    visibleRect = CGRectMake(cellWidth * curIndexPath.row - cellWidth, 0, cellWidth, cellHeight );
    [self.collectionView scrollRectToVisible:visibleRect animated:YES];
}

@end
